﻿Imports System.Windows.Forms

Public Class frmSort

    Private Sub btnAdd_Click(sender As Object, e As System.EventArgs) Handles btnAddAsc.Click, btnAddDesc.Click
        lstSort.Items.Add(lstAvailable.Text & IIf(sender Is btnAddAsc, " ASC", " DESC"))
        FillAvailableList()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnRemove_Click(sender As Object, e As System.EventArgs) Handles btnRemove.Click
        lstSort.Items.Remove(lstSort.Text)
        FillAvailableList()
    End Sub

    Private Sub btnRemoveAll_Click(sender As Object, e As System.EventArgs) Handles btnRemoveAll.Click
        lstSort.Items.Clear()
        FillAvailableList()
    End Sub

    Private Sub FillAvailableList()
        lstAvailable.Items.Clear()
        For Each c As DataColumn In Project.GetTableTemplate(clsProject.enumTableTypes.Working).Columns
            If Not (c.ColumnName.ToUpper = "RECID" OrElse
                    lstSort.Items.Contains(c.ColumnName & " ASC") OrElse
                    lstSort.Items.Contains(c.ColumnName & " DESC")) Then lstAvailable.Items.Add(c.ColumnName)
        Next
    End Sub

    Private Sub frmSort_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        FillAvailableList()
    End Sub

    Private Sub lstAvailable_DoubleClick(sender As Object, e As System.EventArgs) Handles lstAvailable.DoubleClick
        btnAddAsc.PerformClick()
    End Sub

    Private Sub lstAvailable_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstAvailable.SelectedIndexChanged
        btnAddAsc.Enabled = lstAvailable.SelectedIndex <> -1
        btnAddDesc.Enabled = btnAddAsc.Enabled
    End Sub

    Private Sub lstSort_DoubleClick(sender As System.Object, e As System.EventArgs) Handles lstSort.DoubleClick
        btnRemove.PerformClick()
    End Sub

    Private Sub lstSort_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lstSort.SelectedIndexChanged
        btnRemove.Enabled = lstSort.SelectedIndex <> -1
    End Sub
End Class
